package what.basic.table;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.table.*;

import java.awt.event.*;
//import what.basic.basiccomponent.*;
public class Jdefaulttablecellrenderer extends JFrame {
	Object[][] st={
			{"wangpeng",new Integer(91),new Integer(100)}
			,{"zhuxuelian",new Integer(82),new Integer(69)},
			{"meiting",new Integer(47),new Integer(57)},
	};
	String[] names={"name","yuwen","shuxue"};
	private JScrollPane sp=new JScrollPane();
	private DefaultTableModel dtm;
	JTable table;
	public Jdefaulttablecellrenderer(){
				config();//setRenderColor();
		addListener();
		confcolor();

	}
	public void config(){
		table =new JTable (dtm=new DefaultTableModel(st,names));
		DefaultTableCellRenderer tcr=new DefaultTableCellRenderer(){
			public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int col)
			{
				if(row%2==0)
					setBackground(Color.blue);
				else if(row %2==1)
					setBackground(new Color(206,231,255));
				return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
			}
		};
		for(int i=0;i<3;i++){
			table.getColumn(names[i]).setCellRenderer(tcr);
		}
		sp.getViewport().setBackground(Color.blue);
		sp.getViewport().add(table);
		getContentPane().add(sp,BorderLayout.CENTER);
		}
	private void setRenderColor(){
		for(int i=0;i<table.getColumnModel().getColumnCount();i++)
			table.getColumn(names[i]).setCellRenderer(new DefaultTableCellRenderer());
	}
	private void confcolor(){
		setTitle("editor test");
		setSize(500,400);
		setVisible(true);
	}

	private void addListener(){
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				new JFrame().setVisible(false);
				dispose();
			}
		});
	}
public static void main(String[] args) {
	new Jdefaulttablecellrenderer();
	}

}
